package com.mj.haier.rrskx.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.rrskx.domain.BdSwitcher;
import com.mj.haier.rrskx.repository.BdSwitcherRepository;
import com.mj.haier.rrskx.repository.search.BdSwitcherSearchRepository;
import com.mj.haier.rrskx.web.rest.errors.BadRequestAlertException;
import com.mj.haier.rrskx.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing BdSwitcher.
 */
@RestController
@RequestMapping("/api")
public class BdSwitcherResource {

    private final Logger log = LoggerFactory.getLogger(BdSwitcherResource.class);

    private static final String ENTITY_NAME = "microRrskxBdSwitcher";

    private final BdSwitcherRepository bdSwitcherRepository;

    private final BdSwitcherSearchRepository bdSwitcherSearchRepository;

    public BdSwitcherResource(BdSwitcherRepository bdSwitcherRepository, BdSwitcherSearchRepository bdSwitcherSearchRepository) {
        this.bdSwitcherRepository = bdSwitcherRepository;
        this.bdSwitcherSearchRepository = bdSwitcherSearchRepository;
    }

    /**
     * POST  /bd-switchers : Create a new bdSwitcher.
     *
     * @param bdSwitcher the bdSwitcher to create
     * @return the ResponseEntity with status 201 (Created) and with body the new bdSwitcher, or with status 400 (Bad Request) if the bdSwitcher has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/bd-switchers")
    @Timed
    public ResponseEntity<BdSwitcher> createBdSwitcher(@RequestBody BdSwitcher bdSwitcher) throws URISyntaxException {
        log.debug("REST request to save BdSwitcher : {}", bdSwitcher);
        if (bdSwitcher.getSwitcherValue() != null) {
            throw new BadRequestAlertException("A new bdSwitcher cannot already have an SwitcherValue", ENTITY_NAME, "switchervalueexists");
        }
        BdSwitcher result = bdSwitcherRepository.save(bdSwitcher);
        bdSwitcherSearchRepository.save(result);
        return ResponseEntity.created(new URI("/api/bd-switchers/" + result.getSwitcherValue()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getSwitcherValue().toString()))
            .body(result);
    }

    /**
     * PUT  /bd-switchers : Updates an existing bdSwitcher.
     *
     * @param bdSwitcher the bdSwitcher to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated bdSwitcher,
     * or with status 400 (Bad Request) if the bdSwitcher is not valid,
     * or with status 500 (Internal Server Error) if the bdSwitcher couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/bd-switchers")
    @Timed
    public ResponseEntity<BdSwitcher> updateBdSwitcher(@RequestBody BdSwitcher bdSwitcher) throws URISyntaxException {
        log.debug("REST request to update BdSwitcher : {}", bdSwitcher);
        if (bdSwitcher.getSwitcherValue() == null) {
            throw new BadRequestAlertException("Invalid SwitcherValue", ENTITY_NAME, "switchervaluenull");
        }
        BdSwitcher result = bdSwitcherRepository.save(bdSwitcher);
        bdSwitcherSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bdSwitcher.getSwitcherValue().toString()))
            .body(result);
    }

    /**
     * GET  /bd-switchers : get all the bdSwitchers.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of bdSwitchers in body
     */
    @GetMapping("/bd-switchers")
    @Timed
    public List<BdSwitcher> getAllBdSwitchers() {
        log.debug("REST request to get all BdSwitchers");
        return bdSwitcherRepository.findAll();
    }

    /**
     * GET  /bd-switchers/:switcherValue : get the "switcherValue" bdSwitcher.
     *
     * @param switcherValue the switcherValue of the bdSwitcher to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the bdSwitcher, or with status 404 (Not Found)
     */
    @GetMapping("/bd-switchers/{switcherValue}")
    @Timed
    public ResponseEntity<BdSwitcher> getBdSwitcher(@PathVariable String switcherValue) {
        log.debug("REST request to get BdSwitcher : {}", switcherValue);
        Optional<BdSwitcher> bdSwitcher = bdSwitcherRepository.findBySwitcherValue(switcherValue);
        return ResponseUtil.wrapOrNotFound(bdSwitcher);
    }

    /**
     * DELETE  /bd-switchers/:switcherValue : delete the "switcherValue" bdSwitcher.
     *
     * @param switcherValue the switcherValue of the bdSwitcher to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/bd-switchers/{switcherValue}")
    @Timed
    public ResponseEntity<Void> deleteBdSwitcher(@PathVariable String switcherValue) {
        log.debug("REST request to delete BdSwitcher : {}", switcherValue);

        bdSwitcherRepository.deleteBySwitcherValue(switcherValue);
        bdSwitcherSearchRepository.deleteBySwitcherValue(switcherValue);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, switcherValue.toString())).build();
    }

    /**
     * SEARCH  /_search/bd-switchers?query=:query : search for the bdSwitcher corresponding
     * to the query.
     *
     * @param query the query of the bdSwitcher search
     * @return the result of the search
     */
    @GetMapping("/_search/bd-switchers")
    @Timed
    public List<BdSwitcher> searchBdSwitchers(@RequestParam String query) {
        log.debug("REST request to search BdSwitchers for query {}", query);
        return StreamSupport
            .stream(bdSwitcherSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .collect(Collectors.toList());
    }

}
